<?php

/**
 * 公共方法
 * File Name：PublicController.php
 * File Encoding：UTF-8
 * File New Time：2014-4-29 15:50:31
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class PublicController extends AdminBase {

    //特殊的控制器方法
    public function actions() {
        return array(
            //验证码
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'transparent' => true, //是否使用透明背景
                'maxLength' => 6, // 最多生成几个字符
                'minLength' => 4, // 最少生成几个字符
            ),
        );
    }

    //显示后台登录界面
    public function actionLogin() {
        if (IS_POST) {
            $username = Yii::app()->request->getParam('username');
            $password = Yii::app()->request->getParam('password');
            if (empty($username)) {
                $this->error('用户名不能为空！');
            }
            if (empty($password)) {
                $this->error('请输入密码！');
            }
            if (!$this->validateVerifyCode(Yii::app()->request->getParam('code'))) {
                $this->error('验证码错误，请重新输入！');
            }
            //验证登录
            if (Yii::app()->passport->loginAdmin($username, $password)) {
                //$this->success('登录成功！', self::U('index/index'));
                $this->redirect(array('index/index'));
            } else {
                $error = Yii::app()->passport->error ? Yii::app()->passport->error : '登录失败！';
                $this->error($error);
            }
        } else {
            //如果已经登录，直接跳转到后台首页
            if (AdminBase::$uid) {
                $this->redirect(array('index/index'));
            }
            $this->render('login');
        }
    }


}
